package com.tianbing.adlottery.controller.storeadmin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;

import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.User2Site;
import com.tianbing.adlottery.controller.AdLotterySimpleFormController;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditCompanyProductRequestController extends AdLotterySimpleFormController {
	static Logger logger = Logger.getLogger(EditCompanyProductRequestController.class.getName());

	private IUserDao userDao;

	private ISiteDao siteDao;

	private IUserBusiness userBusiness;

	private ICategoryDao categoryDao;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		ControllerFormBean fb = new ControllerFormBean();
		Site site = siteDao.findSiteBySysId(ssid);
		User2Site u2s = new User2Site();
		u2s.setSite(site);
		fb.setUser2Site(u2s);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		ModelAndView mv = new ModelAndView(this.getSuccessView() + "?ssid=" + ssid);
		Map model = mv.getModel();
		ControllerFormBean form = (ControllerFormBean) cmd;
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);
		User2Site s2c = form.getUser2Site();

		userBusiness.saveUserCompanyProductRequest(auth.getUserSysId(), s2c);
		model.put("requestCompanyProductSuccess", "true");
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		return model;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		EditCompanyProductRequestController.logger = logger;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
}
